<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'authSet.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">

		<link rel="stylesheet"
			href="${pageContext.request.contextPath}/css/zTreeStyle/zTreeStyle.css"
			type="text/css">
		<script type="text/javascript"
			src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
		<script type="text/javascript"
			src="${pageContext.request.contextPath}/js/jquery.ztree.all-3.5.min.js"></script>
		<SCRIPT type="text/javascript">
	var setting = {
		async : {
			enable : true,
			url : "${pageContext.request.contextPath}/initRole.action?r=" + Math.random()
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback: {
		   onClick: roleClick
	    }
		
	};
	
	var setting2 = {
		async : {
			enable : true,
			url : "${pageContext.request.contextPath}/initAuth.action?r=" + Math.random()
		},
		check: {
				enable: true
			},
		data : {
			simpleData : {
				enable : true
			}
		}
	};

	$(document).ready(function() {
		$.fn.zTree.init($("#roleTree"), setting);
		$.fn.zTree.init($("#authTree"), setting2);
	});
	
	
	function roleClick(event, treeId, treeNode) {
        reCheck(treeNode.id);
    };
	
	
	function reCheck(id){
			    var zTree = $.fn.zTree.getZTreeObj("authTree");
			    zTree.checkAllNodes(false);
			    $.ajax({
			    url:'getAuthByRole.action?role.uuid='+id+"&r="+Math.random(),
			    dataType:"json",
			    success:function(da){
			       $(da).each(function(){
			         var node =  zTree.getNodeByParam("id",this.id, null);
			         zTree.checkNode(node, true, false);
			       });

			    }
			  });
			}
			
			function saveAuth(){
			   var rTree = $.fn.zTree.getZTreeObj("roleTree");
			   var rnodes = rTree.getSelectedNodes();
			   if(rnodes.length==0){
			     alert('请选择要修改的角色');
			     return;
			   }
			   var rid=rnodes[0].id;
			   
			   var zTree = $.fn.zTree.getZTreeObj("authTree");
               var nodes = zTree.getCheckedNodes(true);
               var ids="";
               for (var i=0;i<nodes.length; i++) {
                 ids+=nodes[i].id+",";
               }
               $.ajax({
                 url:"updRoleAuth.action?r="+Math.random(),
                 data:{"ids":ids,"role.uuid":rid},
                 dataType:"text",
                 success:function(d){
                    alert(d)
                 }
               });
			}
	//-->
</SCRIPT>
	</head>

	<body>
		<div>
			<input type="button" value=" 保存修改 " style="border: #333 solid 1px"
				onclick="saveAuth()" />
		</div>
		<div
			style="border: #333 solid 1px; width: 200px; height: 500px; float: left">
			<ul id="roleTree" class="ztree"></ul>
		</div>

		<div
			style="border: #333 solid 1px; width: 600px; height: 500px; float: left; margin-left: 30px">
			<ul id="authTree" class="ztree"></ul>
		</div>

	</body>
</html>
